package com.li.mybatisconfig.config;

import com.li.mybatisconfig.proxy.MyMapperProxy;
import com.li.mybatisconfig.service.Excutor;
import com.li.mybatisconfig.service.impl.ExcutorImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Proxy;


/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/9/2
 * @Version：1.0.0
 */
public class MysqlSession {

    private Logger logger= LoggerFactory.getLogger(MysqlSession.class);

    /**
     * 注入MyConfiguration
     */
    private MyConfiguration myConfiguration=new MyConfiguration();

    private Excutor excutor=new ExcutorImpl();

    public <T> T selectOne(String statment,Object parameter){
        logger.info("我是query的SqlSession!");
        return  excutor.query(statment,parameter);
    }

    public void insertOne(String statment,Object parmeter){
        logger.info("我是save的SqlSession!");
        excutor.save(statment,parmeter);
    }

    public void updateOne(String statment,Object parmeter){
        logger.info("我是modify的SqlSession!");
        excutor.modify(statment,parmeter);
    }
    public void deleteOne(String statment,Object parmeter){
        logger.info("我是remove的SqlSession!");
        excutor.remove(statment,parmeter);
    }

    public <T> T getMapper(Class<T> clazz){
        logger.info("已经通过代理获取对象！");
        // 动态代理
        return  (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new MyMapperProxy(this, myConfiguration));

    }


}
